
<?php 

$db = parse_ini_file("../resources/db.ini");
$gconfig = parse_ini_file("../resources/galleries.ini");
$sconfig = parse_ini_file("../resources/site.ini");

mysql_connect($db['host'],$db['user'],$db['pass']);
@mysql_select_db($db['db']) or die( "Unable to select database");

$siteId = null;
if (isset($_GET["site"])) {
	$siteId = htmlspecialchars($_GET["site"]);
}
$catId = null;
if (isset($_GET["cat"])) {
	$catId = htmlspecialchars($_GET["cat"]);
}

$page = 0;
if (isset($_GET["page"])) {
	$page = htmlspecialchars($_GET["page"]);
}

$offset = $page * 20;


$query='SELECT * FROM gallery';

if ($siteId != null) {
	$query=$query." and site_id = '$siteId'";
}
if ($catId != null) {
	$query=$query." and cat_id = '".$catId."'";
}

$query=$query.' order by added desc limit 20 offset '.$offset;

$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

	$imagedir=mysql_result($result,$i,"imagedir");
	$id=mysql_result($result,$i,"id");
	$fullpath=$gconfig['image-root']."/".$imagedir;
	
	$files = scandir($fullpath);
	$file = $files[count($files)-5];
       
  	echo '<a class="main-image-link" href="show-'.$id.'">';
   	$imagepath = $fullpath."/".$file;
   	
   	$size = getimagesize($imagepath);
   	
    	echo '<div name="image-div" class="image-div ';
    	if ($size[0] > $size[1]) {
    		echo 'image-landscape';
    	}
    	else {
    		echo 'image-portrait';
    	}
    	echo '" style="background-image: url('.$imagepath.')"></div>';
    echo "</a>";
	
	$i++;
}

mysql_close();
?>
